home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / hce.lha / HCE / LibSource / clib / Misc / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-02  |  4.1 KB  |  172 lines

  1. /*
  2.  * This startup code is called from both the CLI and from Workbench.  If
  3.  * from WorkBench, argc is 0 and the argv parameter points to
  4.  * a WBStartup type of structure.
  5.  *
  6.  * Modified by Jason Petty ,marked J.P. */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/alerts.h>
  10. #include <exec/memory.h>
  11. #include <libraries/dosextens.h>
  12. #include <workbench/startup.h>
  13. #include <stdio.h>
  14. #include <fcntl.h>
  15.  
  16. #ifndef EXIT_SUCCESS
  17. #define EXIT_SUCCESS 0L
  18. #endif
  19.  
  20. extern long SysBase;        /* Used by exec functions */
  21. extern long DOSBase;        /* Required by AmigaDos functions. */
  22. extern long AbsExecBase;    /* Address of Exec base. */
  23. extern int errno;
  24. extern int _argc;
  25. extern char **_argv;
  26. long IntuitionBase=0;            /* Need Inutition Lib open. J.P. */
  27.  
  28.  
  29. int Enable_Abort;
  30. struct WBStartup *WBenchMsg;
  31. typedef struct _device DEVICE;
  32. DEVICE *_devtab;
  33.  
  34. void (*_fcloseall)();
  35. void (*_freeall)();
  36. void (*_closeall)();
  37. void (*_MathBaseClose)();
  38. void (*_MathTransClose)();
  39.  
  40. FILE    _iob[OPEN_MAX];    /* stream buffers */
  41.  
  42. static FILE    startup[] =    /* standard stream files */
  43.     {
  44. /* stdin */    {0, NULL, NULL, (_IOREAD | _IOFBF), 0, 0, '\0'},
  45. /* stdout */    {0, NULL, NULL, (_IOWRT | _IOFBF), 1, 0, '\0'},
  46. /* stderr */    {0, NULL, NULL, (_IOWRT | _IOFBF), 2, 0, '\0'}
  47.     };
  48.  
  49.     long
  50. _main(length, ptr)
  51. long length;        /* Length of the command string */
  52. char *ptr;        /* Command line (BCPL format)   */
  53. {
  54.     void exit();
  55.     register FILE *f;
  56.     register int i, rv;
  57.     struct Process *taskPtr;
  58.     struct WBArg *wp;
  59.     extern struct Process *FindTask();
  60.     extern void *OpenLibrary(), *GetMsg(), *AllocMem();
  61.     extern long Input(), Output(), Open();
  62.     extern void CurrentDir(), Alert(), CloseLibrary(), _exit();
  63.  
  64.     /*
  65.      * Let Exec and AmigaDos know where to find things.
  66.      */
  67.     SysBase = AbsExecBase;
  68.     if ( (DOSBase = (long)OpenLibrary( "dos.library", 0L )) == 0 ){
  69.         Alert( AG_OpenLib|AO_DOSLib, 0L );
  70.         _exit( 12L );
  71.         }
  72.  
  73.     /* Open Intuition Library for the alert functions in math.lib. J.P. */
  74.     if (!(IntuitionBase = (long)OpenLibrary("intuition.library",0)))
  75.                           {
  76.                            Alert( AG_OpenLib|AO_Intuition, 0L );                          
  77.                    CloseLibrary( DOSBase );
  78.                            _exit( 12L );
  79.                            }
  80.     /*
  81.      * Allocate space for the device table.  (Used for lower level
  82.      * i/o open(), close(), read(), write() etc. functions.
  83.      */
  84.  
  85.     _devtab = (DEVICE *)malloc( (int)(OPEN_MAX*sizeof(struct _device)) );
  86.     if ( _devtab == (DEVICE *)NULL){
  87.         Alert(AG_NoMemory, 0L);
  88.         CloseLibrary( DOSBase );
  89.         _exit(12L);
  90.     }
  91.  
  92.     _devtab[0].mode = O_RDONLY | O_STDIO;    /* stdin */
  93.     _devtab[1].mode = O_WRONLY | O_STDIO;    /* stdout */
  94.     _devtab[0].fileHandle = Input();
  95.     _devtab[1].fileHandle = Output();
  96.  
  97.     /*
  98.      * Parse the command line.  Method depends on whether we are
  99.      * running under the CLI or under Workbench.
  100.      */
  101.  
  102.     taskPtr = FindTask(0L);
  103.     if (taskPtr->pr_CLI != 0) {
  104.         _cli_parse(taskPtr, length, ptr);
  105.         Enable_Abort = 1;
  106.     }
  107.     else {
  108.         WaitPort(&taskPtr->pr_MsgPort);
  109.         WBenchMsg = (struct WBStartup *)GetMsg(&taskPtr->pr_MsgPort);
  110.         if (WBenchMsg->sm_ArgList) {
  111.             wp = WBenchMsg->sm_ArgList;
  112.             CurrentDir(wp->wa_Lock);
  113.         }
  114.         _wb_parse(taskPtr, WBenchMsg);
  115.         _argv = (char **)WBenchMsg;
  116.     }
  117.  
  118.     if ( _devtab[1].fileHandle ) {
  119.         _devtab[2].fileHandle = Open("*", MODE_OLDFILE);
  120.         _devtab[2].mode |= O_WRONLY;    /* stderr */
  121.     }
  122.  
  123.     /*
  124.      * Initialize device streams.
  125.      */
  126.  
  127.     memcpy( _iob, startup, sizeof startup );
  128.     for(i = 0, f = _iob; i < 3; ++i, ++f)    /* flag device streams */
  129.         if(isatty(f->_file))
  130.             f->_flag |= _IODEV;
  131.  
  132.     main(_argc, _argv);            /* if main() returns... */
  133.     exit(EXIT_SUCCESS);            /* ...exit with OK status */
  134. }
  135.  
  136. /*
  137.  * Cleanup and exit.
  138.  */
  139.  
  140. void exit(status)
  141. int status;        
  142. {
  143.     int fd;
  144.  
  145.     if ( _fcloseall)
  146.         (*_fcloseall)();
  147.  
  148.     if ( _closeall)
  149.         (*_closeall)();
  150.  
  151.     if ( _freeall)
  152.         (*_freeall)(); /* Free any malloc()ed memory */    
  153.  
  154.     if ( _MathBaseClose )
  155.         (*_MathBaseClose)();
  156.  
  157.     if ( _MathTransClose )
  158.         (*_MathTransClose)();
  159.         if ( IntuitionBase )
  160.                 CloseLibrary( IntuitionBase );
  161.  
  162.     if ( DOSBase )
  163.         CloseLibrary( DOSBase );
  164.  
  165.     if ( WBenchMsg != NULL ) {
  166.         Forbid();
  167.         ReplyMsg(WBenchMsg);
  168.     }
  169.  
  170.     _exit ((long)status);
  171. }
  172.